package com.gdlgxy.oa.controller;

import com.gdlgxy.oa.DAO.KindListDao;
import com.gdlgxy.oa.bean.FareList;
import com.gdlgxy.oa.service.FareServer;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.TreeMap;

public class FareCreateCtrl extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置响应的内容类型，防止中文乱码问题
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        // 获取动态应用的根路径
        String contextPath = request.getContextPath();

        PrintWriter out = response.getWriter();

        out.print("  <!DOCTYPE html>");
        out.print("  <html lang='zh-CN'>");
        out.print("  <head>");
        out.print("      <meta charset='UTF-8'>");
        out.print("      <meta http-equiv='X-UA-Compatible' content='IE=edge'>");
        out.print("      <meta name='viewport' content='width=device-width, initial-scale=1.0'>");
        out.print("      <title>添加支出</title>");
        out.print("      <style>");
        out.print("                  h1 {");
        out.print("              padding: 0px 20px;");
        out.print("              border-left: 10px solid #ed515191;");
        out.print("              background-color: rgba(208, 208, 208, 0);");
        out.print("              font-size: 19px;");
        out.print("              line-height: 30px;");
        out.print("              color: cornflowerblue;");
        out.print("              font-weight: bold;");
        out.print("              margin: 5px 0;");
        out.print("          }");
        out.print("          form {");
        out.print("              display: block;");
        out.print("              width: 250px;");
        out.print("              margin: auto;");
        out.print("          }");
        out.print("          li {");
        out.print("              list-style: none;");
        out.print("          }");
        out.print("          ul.IsignUp li{");
        out.print("              margin-top:5px;");
        out.print("          }");
        out.print("          input.inputLine{");
        out.print("              width:226px;");
        out.print("              padding: .375rem .75rem;");
        out.print("              border-radius: .25rem;");
        out.print("              border: 1px solid #ced4da;");
        out.print("          }");
        out.print("          input.inputLine:focus{");
        out.print("              border-style:solid;");
        out.print("              border-color: #03a9f4;");
        out.print("              box-shadow: 0 0 5px #03a9f4;");
        out.print("          }");
        out.print("          input.inputLeft{");
        out.print("              width:130px;");
        out.print("          }");
        out.print("          button,select{");
        out.print("              padding: .375rem .75rem;");
        out.print("              border-radius: .25rem;");
        out.print("              border: 1px solid #ced4da;");
        out.print("          }");
        out.print("          button:hover,select:hover{");
        out.print("              border-color: #03a9f4;");
        out.print("          }");
        out.print("          button:active {");
        out.print("              background:#03a9f4;");
        out.print("          }");
        out.print("      </style>");
        out.print("  <body>");
        out.print("  <h1>添加支出界面</h1>");
        // 以上是固定的
        // 获取学生编号
        int sno = Integer.parseInt(request.getParameter("Sno"));

        out.print("  <form action='"+contextPath+"/fare/create?Sno="+sno+"' method='post'>");
        out.print("      <ul class='IsignUp' style='padding:0px'>");
        out.print("      <li>");
        out.print("                  支出金额：");
        out.print("          <input class='inputLeft inputLine' type='text' id='name' name='Fmoney' placeholder='不输入将自动生成'>");
        out.print("                  支出类型：");
        out.print("          <select class='selectRight inputLine' name='Kno' id='hobby'>");
        // 以上是固定的
        // 获取数据库中的支出种类
        TreeMap<Integer, String> list = KindListDao.getKindList();

        for (Map.Entry<Integer, String> entry : list.entrySet()) {
            out.print("              <option value='"+entry.getKey()+"'>"+entry.getValue()+"</option>");
        }

        // 以下是固定的
        out.print("          </select>");
        out.print("      </li>");
        out.print("      <li>");
        out.print("          <input class='inputLine' style='height:80px;' id='remark' name='Fremark' placeholder='请输入您想说的备注！'/>");
        out.print("      </li>");
        out.print("      <li>");
        out.print("          <button class='inputLine' type='submit' class='i-button' onclick='addOneToOne()'>");
        out.print("                  确认添加");
        out.print("                  </button>");
        out.print("      </li>");
        out.print("  </ul>");
        out.print("  </form>");
        out.print("  </body>");
        out.print("  </html>");

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取要添加的学生姓名
        // 注意乱码问题
        request.setCharacterEncoding("UTF-8");

        // 获取添加的学生姓名
        int sno = Integer.parseInt(request.getParameter("Sno"));
        // 输入的金额判断
        String buffer = request.getParameter("Fmoney");
        double fmoney = 0;
        if (!buffer.isEmpty()) {
            fmoney = Double.parseDouble(buffer);
        }

        int kno = Integer.parseInt(request.getParameter("Kno"));
        String fremark = request.getParameter("Fremark");
        System.out.println("controller：添加的支出对应的学生编号为："+sno);
        System.out.println("controller：添加的支出金额为："+fmoney);
        System.out.println("controller：添加的支出类型号为："+kno);
        System.out.println("controller：添加的支出备注为："+fremark);

        // 获取对应学生的支出节点
        FareList list = FareServer.list(sno);

        // 判断是否需要随机生成金额
        if (fmoney == 0) {
            // 生成随机金额
            list.fnameAdd(kno,fremark);
        }else{
            // 用户指定金额
            list.fnameAdd(kno,fmoney,fremark);
        }
        // 向Server层传递链表
        if (FareServer.create(list)) {
            request.getRequestDispatcher("/fare/list?Sno=" + sno).forward(request, response);
        } else {
            // 删除失败
            request.getRequestDispatcher("/error.html").forward(request, response);
        }
    }
}
